﻿using System.Collections.Generic;
using System;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.EventSystems;
using Cysharp.Threading.Tasks;

namespace Client.Hotfix
{
	public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerUpHandler, IPointerDownHandler, IPointerEnterHandler, IPointerExitHandler, ISelectHandler, IUpdateSelectedHandler
	{
		public delegate void UIEventHandle();
		public UIEventHandle onClick;
		public UIEventHandle onDown;
		public UIEventHandle onEnter;
		public UIEventHandle onExit;
		public UIEventHandle onUp;
		public UIEventHandle onSelect;
		public UIEventHandle onUpdateSelect;
		public UIEventHandle onDoubleClick;

		[SerializeField] private bool enableSound =false;
		private string soundName = "common_click";
        private int clickInterval = 0;
        private bool canClick =true;

        public static UIEventListener Get(GameObject go, bool enableSound = true, int clickInterval = 200)
		{
			UIEventListener listener = go.GetComponent<UIEventListener>();
			if (listener == null) listener = go.AddComponent<UIEventListener>();
            listener.enableSound = enableSound;
            listener.clickInterval = clickInterval;
            return listener;
		}

        public static UIEventListener Get(GameObject go,string soundName, bool enableSound = true, int clickInterval = 200)
        {
            UIEventListener listener = go.GetComponent<UIEventListener>();
            if (listener == null) listener = go.AddComponent<UIEventListener>();
            listener.enableSound = enableSound;
            listener.soundName = soundName;
            listener.clickInterval = clickInterval;
            return listener;
        }


        public async void OnPointerClick(PointerEventData eventData)
		{
			if (onClick != null)
			{
                if (clickInterval > 0)
                {
                    if (canClick)
                    {
                        canClick = false;
                        await UniTask.Delay((clickInterval));
                        canClick = true;
                    }
                    else
                    {
                        return;
                    }
                }

                if (enableSound)
                {
                    //播放音效
                    Api.Instance.Get<SoundService>().PlayEffectSound("common_click");
                }
                onClick.Invoke();
            }
        }

        public async void OnPointerDown(PointerEventData eventData)
        {
      
			if (onDown != null)
			{
                if (clickInterval > 0)
                {
                    if (canClick)
                    {
                        canClick = false;
                        await UniTask.Delay((clickInterval));
                        canClick = true;
                    }
                    else
                    {
                        return;
                    }
                }
                if (enableSound)
                {
                    //播放音效
                    Api.Instance.Get<SoundService>().PlayEffectSound("common_click");
                }
                onDown.Invoke();
            }
           
        }

        public void OnPointerUp(PointerEventData eventData)
		{
            onUp?.Invoke();
        }

		public async void OnPointerEnter(PointerEventData eventData)
		{
			if (onEnter != null)
			{
                if (clickInterval > 0)
                {
                    if (canClick)
                    {
                        canClick = false;
                        await UniTask.Delay((clickInterval));
                        canClick = true;
                    }
                    else
                    {
                        return;
                    }
                }
                if (enableSound)
                {
                    //播放音效
                    Api.Instance.Get<SoundService>().PlayEffectSound("common_click");
                }
                onEnter.Invoke();
            }

		}

		public void OnPointerExit(PointerEventData eventData)
		{
			onExit?.Invoke();
		}

		public void OnSelect(BaseEventData eventData)
		{
			onSelect?.Invoke();
		}

		public void OnUpdateSelected(BaseEventData eventData)
		{
			onUpdateSelect?.Invoke();
		}

	}
}